package team314;

import java.util.HashSet;

import team314.Navigator.MovementInstruction;
import team314.Navigator.MovementStatus;

import battlecode.common.*;
import static battlecode.common.GameConstants.*;

public class RobotPlayer implements Runnable 
{	
	/**
	 * Constant caches
	 */
	private final RobotController myRC;
	private final RobotType type;
	private final Navigator navigator;
	private final Team myTeam;
	private final double myMaxEnergonLevel; 
	private final boolean iCanAttackAir;
	private final boolean iCanAttackGround;
	private final int myID;
	private final Robot myRobot;
	
	private void debug_message(String s)
	{
		System.out.println(s);
	}
	
	private void debug_bytecode()
	{
		System.out.println("Moving: " + myRC.isMovementActive() + " Bytecode Remaining: " 
				+ String.valueOf(GameConstants.BYTECODES_PER_ROUND - Clock.getBytecodeNum()));
	}
	
	public RobotPlayer(RobotController rc)
	{
	    myRC = rc;	    
	    myTeam = myRC.getTeam();
	    type = myRC.getRobotType();
	    myMaxEnergonLevel = myRC.getMaxEnergonLevel();
	    iCanAttackAir = myRC.canAttackAir();
	    iCanAttackGround = myRC.canAttackGround();
	    myRobot = myRC.getRobot();
	    myID = myRobot.getID();
	    navigator = new Navigator(type, rc);	    
	}			
	
	private boolean taskC = false;
	private boolean alldone = false;
	
	@Override
	public void run() 
	{		
		try
		{
			while (true)
			{				
				if (navigator.mvmtInstr == MovementInstruction.IDLE && !alldone)
				{
					if (!taskC)
						navigator.moveToLocation(new MapLocation(navigator.origX - 40, navigator.origY - 40));
					else
						navigator.moveToLocation(new MapLocation(navigator.origX, navigator.origY));
				}
				
				debug_bytecode();
				
				navigator.navigate();
				
				debug_bytecode();
				debug_message("");
				
				if (!taskC)
				{
					if (navigator.mvmtStatus == MovementStatus.SUCCESS)
						taskC = true;
					else if (navigator.mvmtStatus == MovementStatus.FAILURE)
						debug_message("Tower failed");
				}
				else
				{
					if (navigator.mvmtStatus == MovementStatus.SUCCESS)
					{
						//debug_message("At base");
						//alldone = true;
					}
					else if (navigator.mvmtStatus == MovementStatus.FAILURE)
						debug_message("Base failed");
				}
				
				endRound();
			}
		}
		catch(GameActionException e)
		{
			e.printStackTrace();
			myRC.breakpoint();
		}		
	}	
	
	private void endRound() throws GameActionException
	{
		myRC.yield();
		navigator.update();
	}

}
